<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Enrollment_transaction Extends MY_Controller
{
	private $auth_to_update_fees;
	private $current_session_user;
	public function __construct()
	{
		parent::__construct();
		//laod acl_config
		$this->config->load('acl_config');
		$this->page_auth->secure_page($this->config->item('enrollment_transaction_slip'));
		$this->auth_to_update_fees = $this->config->item('enrollment_transaction_slip');
		$this->current_session_user = strtolower($this->session->userdata('userType'));
		$this->load->model(array('M_enrollment_transactions'));
		$this->load->helper('misc');
		$this->_set_config_items();
		$this->view_data['system_message'] = $this->_msg();

	}
	
	/**** Create ****/
	public function add_enrollment_transaction($e_id='')
	{
		if(is_numeric($e_id) AND $e_id !== FALSE)
		{
			if($this->M_enrollment_transactions->get_enrollment_transaction($e_id) == FALSE){
			$this->view_data['process_to_be'] = 'add';
			$this->view_data['enrollment_id'] = $e_id;
			$this->view_data['assesment'] = 'yes';
			$this->view_data['stat'] = 'regular';
			$this->view_data['requirements_for_admission'] = '';
			$this->view_data['star_program'] = 'one-on-one';
			$this->view_data['interview'] = 'yes';
			$this->view_data['requirements_checked'] = 'complete';
			$this->view_data['requirements_checked_remarks'] = '';
			$this->view_data['payment'] = 'yes';
			if($this->input->post('add_enrollment_transaction') !== FALSE)
			{
				$v = (object)$this->input->post();
				
				$e_t_s_data['e_id'] = $v->enrollment_id;
				$e_t_s_data['assesment'] = $v->assesment;
				$e_t_s_data['stat'] = $v->stat;
				$e_t_s_data['requirements_for_admission'] = $v->requirements_for_admission;
				$e_t_s_data['star_program'] = $v->star_program;
				$e_t_s_data['interview'] = $v->interview;
				$e_t_s_data['requirements_checked'] = $v->requirements_checked;
				$e_t_s_data['requirements_checked_remarks'] = $v->requirements_checked_remarks;
				$e_t_s_data['payment'] = $v->payment;
				
				if($this->M_enrollment_transactions->insert_enrollment_transaction($e_t_s_data) == TRUE){
					$this->_msg('s','Successfully added enrollment transaction to Student. ','enrollment_transaction/add_enrollment_transaction/'.$e_id);
				}else{
					$this->_msg('e','System encountered an error while adding enrollment transaction to student. ','enrollment_transaction/add_enrollment_transaction/'.$e_id);
				}
			}
			}else{
			$this->view_data['process_to_be'] = 'update';
			$this->view_data['enrollment_id'] = $e_id;
			$this->view_data['data'] = $this->M_enrollment_transactions->get_enrollment_transaction($e_id);
			if($this->input->post('update_enrollment_transaction') !== FALSE)
			{
				$v = (object)$this->input->post();
				
				$e_t_s_data['e_id'] = $v->enrollment_id;
				$e_t_s_data['assesment'] = $v->assesment;
				$e_t_s_data['stat'] = $v->stat;
				$e_t_s_data['requirements_for_admission'] = $v->requirements_for_admission;
				$e_t_s_data['star_program'] = $v->star_program;
				$e_t_s_data['interview'] = $v->interview;
				$e_t_s_data['requirements_checked'] = $v->requirements_checked;
				$e_t_s_data['requirements_checked_remarks'] = $v->requirements_checked_remarks;
				$e_t_s_data['payment'] = $v->payment;
				
				if($this->M_enrollment_transactions->update_enrollment_transaction($e_t_s_data,$v->enrollment_id) == TRUE){
					$this->_msg('s','Successfully updated enrollment transaction to Student. ','enrollment_transaction/add_enrollment_transaction/'.$e_id);
				}else{
					$this->_msg('e','System encountered an error while updating enrollment transaction to student. ','enrollment_transaction/add_enrollment_transaction/'.$e_id);
				}
			}
			}
		}
		else
		{
			show_error('Nothing To Show,',404);
		}
	}
	
	/*
	public function view_fees($e_id='')
	{
		if(!is_numeric($e_id))
		{
			redirect($this->current_session_user);
		}else{
			$this->load->model(array('M_enrollees','M_scholarships','M_fees'));
			
			$this->view_data['payment_mode'] = $this->M_enrollees->get_student_payment_mode($e_id);
			$this->view_data['authlink'] = $this->authlink->generate_authlink();
			$this->view_data['student_deductions'] = $this->M_scholarships->get_student_deductions($e_id);
			$this->view_data['total_stud_deductions'] = $this->M_scholarships->sum_all_student_deductions($e_id);
			$this->view_data['system_message'] = $this->session->flashdata('system_message');
			
			$this->view_data['other_fees'] = $this->M_enrollees->get_student_fees($e_id,$this->current_school_year,'other');
			$this->view_data['tuit_fees'] = $this->M_enrollees->get_student_fees($e_id,$this->current_school_year,'tuit');
			$this->view_data['misc_fees'] = $this->M_enrollees->get_student_fees($e_id,$this->current_school_year,'misc');
			$this->view_data['elec_fees'] = $this->M_enrollees->get_student_fees($e_id,$this->current_school_year,'elec');
			$this->view_data['uniform'] = $this->M_enrollees->get_student_fees($e_id,$this->current_school_year,'uniform');
			
			$this->view_data['modal']['scholarships'] = $this->M_scholarships->get_scholarships_available_for_stud($e_id);
			$this->view_data['modal']['id'] = $e_id;
			
			$this->view_data['enrollee_data'] = $this->M_enrollees->get_enrollee_prof($e_id);
			$this->view_data['sum_paid_fees'] = $this->M_enrollees->get_sum_of_paid_fees($e_id,$this->current_school_year);
			$this->view_data['sum_amount_due'] = $this->M_enrollees->get_sum_of_amount_due($e_id,$this->current_school_year);
			$this->view_data['profile_id'] = NULL;
			$this->view_data['enrollment_id'] = $e_id;
			$this->view_data['level_id'] = $this->view_data['enrollee_data']->level_id;
			
			$payment_data['amount_due'] = $this->view_data['sum_amount_due'];
			$payment_data['amount_paid'] = $this->view_data['sum_paid_fees'];
			
			if((!empty($payment_data['amount_due']) AND !empty($payment_data['amount_paid'])) AND ($payment_data['amount_due'] - $payment_data['amount_paid']) == 0)
			{
				$this->M_student_payment_record->update_fully_paid_status($e_id);
				$this->view_data['finance_status'] = $this->_msg('n','Student Is Already Full Paid');
			}
			
			
			$this->session->set_userdata('payment_data','');
			$this->session->set_userdata('payment_data',(object)$payment_data);
			
			if(in_array($this->current_session_user,$this->auth_to_update_fees))
			{
				$this->view_data['menu_true'] = TRUE;
				// $this->view_data['add_edit_deductions'] =function($a,$auth){return '<a href="'.site_url('fees/deductions/'.$a.'/'.$auth).'" class="btn btn-mini btn-success">Add</a> |<a href="'.site_url('fees/show_deductions/'.$a).'" class="btn btn-mini btn-success">Edit</a>';};
				$this->view_data['add_edit_deductions'] =function($a,$auth){return '<a href="" data-reveal-id="main-modal-menu" class="btn btn-mini btn-success">Add</a> |<a href="'.site_url('fees/show_deductions/'.$a).'" class="btn btn-mini btn-success">Edit</a>';};
				$this->view_data['delete_link'] = function($a,$b,$c){return'<td><a href="'.site_url('fees/delete_stud_fee/'.$a.'/'.$b.'/'.$c).'" class="btn btn-mini btn-danger confirm" title="Are you sure you want to delete fee from record? Changes cannot be reverted. continue?"><i class="icon-remove"></i></a></td>';};
			}else{
				$this->view_data['menu_true'] = FALSE;
				$this->view_data['add_edit_deductions'] = function(){return NULL;};
				$this->view_data['delete_link'] = function(){return '<td>&nbsp;</td>';};
			}
			
			if($this->input->post('add_deduction'))
			{
				pd($this->input->post());
			}
			
			//process students scholarships
			if($this->input->post('add_scholarship'))
			{
				$result = $this->_process_student_scholarship($e_id,$this->input->post('scholarship_id'));

				if($result->status == TRUE)
				{
					$this->_msg('s',$result->message,'fees/view_fees/'.$e_id);
				}else{
					$this->_msg('e',$result->message,'fees/view_fees/'.$e_id);
				}
			}
			
			$this->view_data['add_uniform'] = $this->M_fees->get_fee_array('uniform');
		}
	}
	
	public function check_pull_tuition_fee($e_id,$a)
	{
		if(is_numeric($e_id))
		{	
			$e_id = intval($e_id);
			
			if($this->authlink->verify_authlink($a))
			{
				$this->load->model('M_level_fees','mlf');
				$this->load->model('M_enrollees','e');
				$this->load->model('M_new_enrollee','n');
				
				$data = $this->e->get_enrollee_prof($e_id);
				
				if($data !== FALSE)
				{
					$tuition_fees = $this->mlf->get_tuition_fees_set_for_current_level($data->level_id);
					if($tuition_fees !== FALSE)
					{	
						if($this->n->set_tuition_fees_set_for_current_level($tuition_fees,$data->level_id,$e_id,$this->current_grading_period,$this->current_school_year) == TRUE)
						{
							$this->authlink->clear_authlink();
							$this->_msg('s','Process was successful, Added Tuition to student account details.','fees/view_fees/'.$e_id);
						}else{
							$this->authlink->clear_authlink();
							$this->_msg('e','System encountered an Error while adding Miscellaneous Fees to student account details.','fees/view_fees/'.$e_id);
						}		
					}else{
						$this->authlink->clear_authlink();
						$this->_msg('e','Please COntact School Administrator, Tuition fees for '.ucwords($data->level_desc).' has not been set.','fees/view_fees/'.$e_id);
					}
				}else{
					$this->authlink->clear_authlink();
					$this->_msg('e','Error!, Missing Student Credentials on the system.','fees/view_fees/'.$e_id);
				}
			}else{
				redirect($this->current_session_user);
			}
		}
	}
	
	public function check_pull_miscellaneous_fee($e_id,$a)
	{
		if(is_numeric($e_id))
		{	
			$e_id = intval($e_id);
			
			if($this->authlink->verify_authlink($a))
			{
				$this->load->model('M_level_fees','mlf');
				$this->load->model('M_enrollees','e');
				$this->load->model('M_new_enrollee','n');
				$data = $this->e->get_enrollee_prof($e_id);
				
				if($data !== FALSE)
				{
					$misc_fees = $this->mlf->get_misc_fees($e_id);
					if($misc_fees !== FALSE)
					{	
						if($this->n->insert_misc_fee($misc_fees,$e_id,$this->current_school_year) == TRUE)
						{
							$this->authlink->clear_authlink();
							$this->_msg('s','Miscellaneous Fees is successfully added to student account details.','fees/view_fees/'.$e_id);
						}else{
							$this->authlink->clear_authlink();
							$this->_msg('e','System Errorm, cannot add Miscellaneous Fees to student details.','fees/view_fees/'.$e_id);
						}		
					}else{
						$this->authlink->clear_authlink();
						$this->_msg('e','Please Contact School Administrator, Miscellaneous fees for '.ucwords($data->level_desc).' has not been set.','fees/view_fees/'.$e_id);
					}
				}else{
					$this->authlink->clear_authlink();
					$this->_msg('e','Error!, Missing Student Credentials on the system.','fees/view_fees/'.$e_id);
				}
			}else{
				redirect($this->current_session_user);
			}
		}
	}
	
	public function payment_record($id = FALSE,$pid = FALSE)
	{
		if(is_numeric($id) AND $id !== FALSE)
		{	
			$this->load->model('M_student_payment_record','mspr');
			$this->view_data['payments_made'] = $this->mspr->get_payment_record_by_enrollment_id($id);
			if(!empty($this->view_data['payments_made']))
			{
				$this->view_data['system_message'] = $this->session->flashdata('system_message');
				$this->view_data['authlink'] = $this->authlink->generate_authlink();
				$id = intval($id);
				$this->view_data['enrollee_data'] = $this->M_enrollees->get_enrollee_prof($id);
				$this->view_data['enrollment_id'] = $id;
				$this->view_data['rdate'] = function($x){return date('M d, Y h:i A',strtotime($x));};
				
				if(is_numeric($pid))
				{
					$this->load->model('M_student_payment_breakdown','mspb');
					$this->view_data['payment_breakdown'] = $this->mspb->get_payment_breakdown_by_spr_id($pid);
				}
			}else{
				$this->_msg('n','Student Has No payment record yet','fees/view_fees/'.$id);
			}
		}else{
			redirect($this->current_session_user);
		}
	}
	
	public function add_fee($enrollment_id = FALSE)
	{
		if($enrollment_id == FALSE)
		{
			if($this->input->post('cashier_add_fee') == FALSE)
			{
				redirect($this->current_session_user);
			}else
			{
				$enrollment_id_post = $this->input->post('enrollment_id',TRUE);
				$c_spr_id = $this->M_student_payment_record->current_payment_record_id($enrollment_id_post);
					
				$counter =0;
				foreach($this->input->post('fee') as $key => $value)
				{
					foreach($value as $rate => $name)
					{
						$data[$counter]['sef_fee_name'] = $name;
						$data[$counter]['sef_fee_rate'] = $rate;
						$data[$counter]['sef_enrollment_id'] = $enrollment_id_post;
						$data[$counter]['sef_gperiod_id'] = $this->current_grading_period;
						$data[$counter]['sef_schoolyear_id'] = $this->current_school_year;
						$data[$counter]['sef_created'] =NOW;
						$data[$counter]['is_other_fee'] = 1;
							
						if($c_spr_id !== NULL)
						{
								$for_pb[$counter]['spb_fee_name'] =$name;
								$for_pb[$counter]['spb_remaining_balance'] = $rate;
								$for_pb[$counter]['spb_fee_rate'] =$rate;
								$for_pb[$counter]['spb_transacted_by'] =$this->session->userdata('userid');
								$for_pb[$counter]['spb_spr_id'] =$c_spr_id;
								$for_pb[$counter]['spb_created'] = NOW;
								$for_pb[$counter]['spb_stud_enrollment_id'] = $enrollment_id_post;
								$for_pb[$counter]['spb_is_other_fee'] = 1;
						}else{
							$for_pb = FALSE;
						}
						$counter++;
					}
				}
				
				if($this->M_enrollees->cashier_add_new_fees($data,$for_pb) == TRUE){
					$this->_msg('s','Successfully added other fee to student Account details. ','fees/view_fees/'.$enrollment_id_post);
				}else{
					$this->_msg('e','System encountered an Error while adding other fee to student. ','fees/view_fees/'.$enrollment_id_post);
				}
			}
			
		}elseif(is_numeric($enrollment_id))
		{
			$this->view_data['available_fees'] = $this->M_enrollees->get_available_fees($enrollment_id);
			$this->view_data['enrollment_id'] = $enrollment_id;
		}
	}
	
	public function add_elective_fee($enrollment_id = FALSE)
	{
		if($enrollment_id == FALSE)
		{
			if($this->input->post('cashier_add_efee') == FALSE)
			{
				redirect($this->current_session_user);
			}else
			{
				$enrollment_id_post = $this->input->post('enrollment_id',TRUE);
				$c_spr_id = $this->M_student_payment_record->current_payment_record_id($enrollment_id_post);
					
				$counter =0;
				foreach($this->input->post('fee') as $key => $value)
				{
					foreach($value as $rate => $name)
					{
						$data[$counter]['sef_fee_name'] = $name;
						$data[$counter]['sef_fee_rate'] = $rate;
						$data[$counter]['sef_enrollment_id'] = $enrollment_id_post;
						$data[$counter]['sef_gperiod_id'] = $this->current_grading_period;
						$data[$counter]['sef_schoolyear_id'] = $this->current_school_year;
						$data[$counter]['sef_created'] =NOW;
						$data[$counter]['is_elective_fee'] = 1;
							
						if($c_spr_id !== NULL)
						{
								$for_pb[$counter]['spb_fee_name'] =$name;
								$for_pb[$counter]['spb_remaining_balance'] = $rate;
								$for_pb[$counter]['spb_fee_rate'] =$rate;
								$for_pb[$counter]['spb_transacted_by'] =$this->session->userdata('userid');
								$for_pb[$counter]['spb_spr_id'] =$c_spr_id;
								$for_pb[$counter]['spb_created'] = NOW;
								$for_pb[$counter]['spb_stud_enrollment_id'] = $enrollment_id_post;
								$for_pb[$counter]['is_elective_fee'] = 1;
						}else{
							$for_pb = FALSE;
						}
						$counter++;
					}
				}
				
				if($this->M_enrollees->cashier_add_new_efees($data,$for_pb) == TRUE){
					$this->_msg('s','Elective fee was added to student account details','fees/view_fees/'.$enrollment_id_post);
				}else{
					$this->_msg('e','Error, Elective fee was not added to student account details','fees/view_fees/'.$enrollment_id_post);
				}
			}
			
		}elseif(is_numeric($enrollment_id))
		{
			$this->view_data['available_fees'] = $this->M_enrollees->get_elective_fees($enrollment_id);
			$this->view_data['enrollment_id'] = $enrollment_id;
		}
	}
	
	
	
	
	public function add_fee_manually($e_id = FALSE,$a ='')
	{
		if(is_numeric($e_id) AND $this->authlink->verify_authlink($a))
		{
			if($this->input->post('manual_add_fee') !== FALSE)
			{
				$e_id_post = $this->input->post('enrollment_id');
				
				$this->view_data['enrollment_id'] 	= $e_id_post;
				
				$c_spr_id = $this->M_student_payment_record->current_payment_record_id($e_id_post);
					
				$counter =0;
				foreach($this->input->post('fee') as $key => $value)
				{
					foreach($value as $rate => $name)
					{
						$data[$counter]['sef_fee_name'] = $name;
						$data[$counter]['sef_fee_rate'] = $rate;
						$data[$counter]['sef_enrollment_id'] = $e_id_post;
						$data[$counter]['sef_gperiod_id'] = $this->current_grading_period;
						$data[$counter]['sef_schoolyear_id'] = $this->current_school_year;
						$data[$counter]['sef_created'] =NOW;
						$data[$counter]['is_other_fee'] = 1;
							
						if($c_spr_id !== NULL)
						{
							$for_pb[$counter]['spb_fee_name'] =$name;
							$for_pb[$counter]['spb_remaining_balance'] = $rate;
							$for_pb[$counter]['spb_fee_rate'] =$rate;
							$for_pb[$counter]['spb_transacted_by'] =$this->session->userdata('userid');
							$for_pb[$counter]['spb_spr_id'] =$c_spr_id;
							$for_pb[$counter]['spb_created'] =NOW;
							$for_pb[$counter]['spb_stud_enrollment_id'] = $e_id_post;
							$for_pb[$counter]['spb_is_other_fee'] = 1;
						}else{
							$for_pb = FALSE;
						}		
						$counter++;
					}
					
				}
				
				if($this->M_enrollees->cashier_add_new_fees($data,$for_pb) == TRUE){
					$this->authlink->clear_authlink();
					$this->_msg('s','Fees were successfully added','fees/view_fees/'.$e_id_post);
				}else{
					$this->authlink->clear_authlink();
					$this->_msg('e','Fees were not added, Error encountered','fees/view_fees/'.$e_id_post);
				}				
			}else
			{
				$this->load->model('M_fees');
				$this->view_data['fees'] = $this->M_fees->get_all_fees();
				$this->view_data['enrollment_id'] = $e_id;
				$this->view_data['a'] = $a;
			}
		}else{
			redirect('fees/view_fees/'.$e_id);
		}
	}
	
	public function add_payment_record($enrollment_id = FALSE,$authlink = FALSE)
	{
		// if($this->M_enrollees->verify_if_approved_by_registrar($enrollment_id) == TRUE)
		
			$this->view_data['enrollee_data'] = $this->M_enrollees->get_enrollee_prof($enrollment_id);
			$this->view_data['amount_due'] = $this->session->userdata('payment_data');
			if($enrollment_id == FALSE OR $authlink == FALSE)
			{
				if($this->input->post('add_payment_record') !== FALSE)
				{
					$enrollment_id_post = $this->input->post('enrollment_id');
					$authlink_post = $this->input->post('authlink');
					
					if($this->M_enrollees->verify_if_approved_by_registrar($enrollment_id_post) == TRUE)
					{								
						if($this->form_validation->run('add_payment_record') == FALSE)
						{
							$this->view_data['enrollment_id'] = $enrollment_id_post;
							$this->view_data['authlink'] = $authlink_post;
						}else
						{
							// vaerify authlink, anti spam or XSS
							if($this->authlink->verify_authlink($authlink_post) == TRUE)
							{
								$input['spr_user_trans_id'] = $this->session->userdata('userid');
								$input['spr_user_trans_name'] = $this->session->userdata('username');
								$input['spr_enrollment_id'] = $enrollment_id_post;
								$input['spr_or_no'] = $this->input->post('spr_or_no');
								$input['spr_remarks'] = $this->input->post('remarks');
								$input['spr_payment_date'] =  date('Y-m-d',strtotime($this->input->post('date_of_payment')));
								$input['spr_schoolyear_id'] = $this->current_school_year;// defined in mycontroller
								$input['spr_gperiod_id'] = $this->current_grading_period;// defined in mycontroller
								$input['spr_ammt_paid'] = $this->input->post('ammount_paid');
								$input['spr_created'] = NOW;
										
								if($this->M_student_payment_record->add_payment_record($input) == FALSE)
								{
									$this->authlink->clear_authlink();
									$this->_msg('e','Payment Record was not added','fees/view_fees/'.$enrollment_id_post);
								}else
								{	
									if($this->M_student_payment_record->update_enrollment_record($enrollment_id_post) == TRUE)
									{
										$this->_msg('s','Payment Record was successfully added','fees/payment_breakdown/'.$enrollment_id_post.'/'.$authlink_post);
									}else{
										$this->authlink->clear_authlink();
										$this->_msg('e','Payment Record was not added','fees/view_fees/'.$enrollment_id_post);
									}
								}
							}else
							{
								$this->authlink->clear_authlink();
								$this->_msg('e','Token Expired','fees/view_fees/'.$enrollment_id_post);						
							}
						}
					}else{
						$this->authlink->clear_authlink();
						$this->_msg('e','Enrollee Is not yet verified by the registrar.','fees/view_fees/'.$enrollment_id_post);
					}
				}else
				{
					redirect($this->current_session_user);
				}
			}else
			{
				if($this->authlink->verify_authlink($authlink) == TRUE)
				{
					$misc_fees = $this->M_enrollees->get_enrollee_fees($enrollment_id,$this->current_school_year,'misc');
					$tuit_fees = $this->M_enrollees->get_enrollee_fees($enrollment_id,$this->current_school_year,'tuit');
					
					if(!empty($misc_fees) AND !empty($tuit_fees))
					{
						$this->view_data['enrollment_id'] = $enrollment_id;
						$this->view_data['authlink'] = $authlink;
					}else{
						$this->_msg('e','Warning Cannot Add Payment Report, Tuition And Miscellaneous Fees Must be set Before adding Payment Record','fees/view_fees/'.$enrollment_id);
					}
				}else{
					redirect('fees/view_fees/'.$enrollment_id);
				}		
			}
	}
	
	
	public function delete_stud_fee($sef_id = FALSE,$e_id = FALSE,$auth = FALSE)
	{
		if(!is_numeric($e_id) OR !is_numeric($sef_id) OR $auth == FALSE)
		{
			redirect($this->current_session_user);
		}else{
			if($this->authlink->verify_authlink($auth) == TRUE)
			{
				if($this->M_enrollees->delete_stud_fee($sef_id,$e_id) == TRUE)
				{	
					$this->_msg('s','The selected Fees were successfully deleted.','fees/view_fees/'.$e_id);
				}else{
					$this->_msg('e','Selected Fees were not Delete, An Error was Encountered, Please Try Again.','fees/view_fees/'.$e_id);
				}
			}else
			{
				redirect('fees/view_fees/'.$e_id);
			}
		}
	}
	
	public function check_fees($e_id = FALSE,$l_id = FALSE,$auth = FALSE)
	{
		if(($e_id == FALSE AND !(int)$e_id) OR ($l_id == FALSE AND !(int)$l_id) OR $auth == FALSE)
		{	
			if($this->input->post('check_fee_add') !== FALSE)
			{
				$e_id_post = $this->input->post('enrollment_id');
				$counter =0;
				foreach($this->input->post('fee') as $key => $value)
				{
					foreach($value as $rate => $name)
					{
							$data[$counter]['sef_fee_name'] = $name;
							$data[$counter]['sef_fee_rate'] = $rate;
							$data[$counter]['sef_enrollment_id'] = $e_id_post;
							$data[$counter]['sef_gperiod_id'] = $this->current_grading_period;
							$data[$counter]['sef_schoolyear_id'] = $this->current_school_year;
							$data[$counter]['sef_created'] =NOW;
							$data[$counter]['is_other_fee'] =1;
							$counter++;
					}
				}
				
				if($this->M_enrollees->cashier_add_new_fees($data) == TRUE){
					$this->authlink->clear_authlink();
					$this->session->set_flashdata('system_message','<div class="success">Fees were successfully added</div>');
					redirect('fees/view_fees/'.$e_id_post);
				}else{
					$this->authlink->clear_authlink();
					$this->session->set_flashdata('system_message','<div class="error">Fees were not added</div>');
					redirect('fees/view_fees/'.$e_id_post);
				}				
			}else
			{
				redirect($this->current_session_user);
			}
		}else
		{
			if($this->authlink->verify_authlink($auth) == TRUE)
			{
				$this->view_data['fees'] = $this->M_enrollees->check_level_fee_status($l_id);
				$this->view_data['e_id'] = $e_id;
				$this->view_data['l_id'] = $l_id;
				$this->view_data['authlink'] = $auth;
			}else
			{
				redirect($this->current_session_user);
			}
		}
	}
	
	function deductions($e_id='',$auth = FALSE)
	{
		if(is_numeric($e_id))
		{
			$this->load->model('M_scholarships','m');
			$this->view_data['enrollment_id'] = $e_id;
			$this->view_data['auth'] = $auth;
			$this->view_data['scholarships'] = $this->m->get_scholarships_available_for_stud($e_id);
			$this->view_data['system_message'] = $this->session->flashdata('system_message');
			
			if($this->input->post('add_scholarship'))
			{
			
				$this->form_validation->set_rules('scholarships','required|trim|htmlspecialchars');
				$this->form_validation->set_rules('remarks','Remarks','trim|alpha_numeric|htmlspecialchars');
				
				if($this->form_validation->run() !== FALSE)
				{
					$data['scholarships'] = $this->input->post('scholarships');
					$data['remarks'] = $this->input->post('remarks');
					$data['enrollment_id'] = $e_id;
					if($this->m->get_selected_scholarship_and_insert_to_deductions($data))
					{
						$this->authlink->clear_authlink();
						$this->session->set_flashdata('system_message','<div class="success">Scholarship Added to student</div>');
					}else{
						$this->authlink->clear_authlink();
						$this->session->set_flashdata('system_message','<div class="error">Failed to add Scholarship to student</div>');
					}

					redirect('fees/view_fees/'.$e_id);
				}
			}
			
			if($this->input->post('add_deduction'))
			{
				echo 'add deduction';
			}
		}
	}
	
	public function payment_breakdown($enrollment_id,$a)
	{
		if($this->authlink->verify_authlink($a) == TRUE)
		{
			$e_id = intval($enrollment_id);
			$this->load->model(array('M_enrollees','M_student_payment_record'));
			$this->view_data['system_message'] = $this->session->flashdata('system_message');
			$this->view_data['enrollee_data'] = $this->M_enrollees->get_enrollee_prof($enrollment_id);
			
			$this->view_data['other_fees'] = $this->M_enrollees->get_enrollee_fees($e_id,$this->current_school_year,'other');
			$this->view_data['tuit_fees'] = $this->M_enrollees->get_enrollee_fees($e_id,$this->current_school_year,'tuit');
			$this->view_data['misc_fees'] = $this->M_enrollees->get_enrollee_fees($e_id,$this->current_school_year,'misc');
			$this->view_data['elec_fees'] = $this->M_enrollees->get_enrollee_fees($e_id,$this->current_school_year,'elec');
			$this->view_data['uniform'] = $this->M_enrollees->get_enrollee_fees($e_id,$this->current_school_year,'uniform');
			
			
			$payment_id = $this->M_enrollees->get_latest_payment_record_id($e_id);
			
			$this->view_data['payment_record'] = $this->M_student_payment_record->get_payment_record($payment_id);
			
			$this->view_data['enl_id'] = $e_id;
			$this->view_data['pay_id'] = $payment_id;
			$this->view_data['authlink'] = $a;
			
			if($this->input->post('save_payment_breakdown'))
			{
				$data = $this->input->post('fee');
				$mpb = $this->input->post('max_payment_breakdown');
				$this->load->model('M_student_payment_breakdown','mspb');
		
				$result = $this->mspb->insert_payment_breakdown($data,$mpb);
				if($result->status == true)
				{
					$this->authlink->clear_authlink();
					$this->_msg('s',$result->log,'fees/view_fees/'.$enrollment_id);
				}else{
					$this->view_data['system_message'] = '<div class="alert alert-box">'.$result->log.'</div>';
				}
			}
		}else
		{
			redirect($this->current_session_user);
		}
	}
	
	public function delete_payment($eid,$id,$a)
	{
		if($this->authlink->verify_authlink($a) AND is_numeric($id) AND is_numeric($eid))
		{
			$this->load->model('M_student_payment_record','mspr');
			
			if($this->mspr->current_payment_record_id($eid) == $id)
			{
				if($this->mspr->delete_student_payment_and_record($id))
				{
					$this->authlink->clear_authlink();
					$this->_msg('s','Payment Was Successfully Deleted.','fees/payment_record/'.$eid);
				}else{
					$this->authlink->clear_authlink();
					$this->_msg('e','Unable to delete payment record','fees/payment_record/'.$eid);
				}
			}else{
				$this->authlink->clear_authlink();
				$this->_msg('e','Wrong Payment selected','fees/payment_record/'.$eid);
			}
			
		}else{
			redirect($this->current_session_user);
		}
	}
	
	public function edit_payment($eid,$pid,$auth)
	{
		if(is_numeric($eid) AND is_numeric($pid) AND $this->authlink->verify_authlink($auth))
		{
			$this->load->model('M_student_payment_record','mspr');
			if($this->mspr->current_payment_record_id($eid) == $pid)
			{
				$this->view_data['payment'] = $this->mspr->get_payment_record($pid);
				$this->view_data['enrollee_data'] = $this->M_enrollees->get_enrollee_prof($eid);
				$this->view_data['eid'] = $eid;
				$this->view_data['pid'] = $pid;
				$this->view_data['aut'] = $auth;

				if($this->input->post('update_payment_record'))
				{
					$input['spr_payment_date'] = date('Y-m-d',strtotime($this->input->post('date_of_payment')));
					$input['spr_ammt_paid'] = $this->input->post('amount_paid');
					$input['spr_remarks'] = $this->input->post('remarks');
					$p_pid = $this->input->post('payment_id');
					if($this->mspr->update_payment_record($input,$p_pid))
					{
						$this->authlink->clear_authlink();
						$a = $this->authlink->hash_make($p_pid);
						redirect('fees/update_payment_breakdown/'.$eid.'/'.$pid.'/'.$a);
					}else{
						$this->view_data['system_message'] = '<div class="error">Unable to update student payment record</div>';
					}
				}
			}else{
				$this->authlink->clear_authlink();
				$this->session->set_flashdata('system_message','<div class="error">Wrong Payment selected</div>');
				redirect('fees/payment_record/'.$eid);
			}
		}else{
			redirect($this->current_session_user);
		}
	}
	
	public function update_payment_breakdown($eid,$pid,$a)
	{
		if(is_numeric($eid) AND is_numeric($pid) AND $this->authlink->check_hash_make($pid,$a))
		{
			$this->load->model('M_student_payment_breakdown','mspb');
			$this->view_data['payment_record'] = $this->M_student_payment_record->get_payment_record($pid);
			$this->view_data['enrollee_data'] = $this->M_enrollees->get_enrollee_prof($eid);
			$this->view_data['reg_fees'] = $this->mspb->get_specific_breakdown($pid,$eid,'reg');
			$this->view_data['misc_fees'] = $this->mspb->get_specific_breakdown($pid,$eid,'misc');
			$this->view_data['tuit_fees'] = $this->mspb->get_specific_breakdown($pid,$eid,'tuit');
			$this->view_data['pay_id'] = $pid;
			$this->view_data['enl_id'] = $eid;
			$this->view_data['auth'] = $a;
			
			
			if($this->input->post('save_payment_breakdown'))
			{
				if($this->mspb->update_payment_breakdown($this->input->post('fee'),$pid,$eid))
				{
					$this->session->set_flashdata('system_message','<div class="success">Updated payment breakdown</div>');
					redirect('fees/payment_record/'.$eid.'/'.$pid);
				}else{
					$this->session->set_flashdata('system_message','<div class="error">Unable to update breakdown</div>');
					redirect('fees/payment_record/'.$eid);
				}
			}
		}else{
			$this->session->set_flashdata('system_message','<div class="error">Wrong Link</div>');
			redirect('fees/payment_record/'.$eid);
		}
	}
	
	
	public function add_uniform_fee($id = FALSE)
	{
		if($id !== FALSE AND is_numeric($id))
		{
			if($this->input->post('add_uniform'))
			{
				$fee_id = $this->input->post('uniform_fee_id');
				$fee_qty =$this->input->post('uniform_quantity');
				
				$this->load->model('M_fees','mf');
				$ufee = $this->mf->get_all_fees($fee_id);

				$c_spr_id = $this->M_student_payment_record->current_payment_record_id($id);
				
				$name = $ufee[0]->fee_desc.' - ('.$fee_qty.') PCS'; //name of fee and number of quantity
				$rate = ($ufee[0]->fee_rate * $fee_qty); // rate is rate of fee multiplied by quantity 
				
				$data['sef_fee_name'] = $name;
				$data['sef_fee_rate'] = $rate;
				$data['sef_enrollment_id'] = $id;
				$data['sef_gperiod_id'] = $this->current_grading_period;
				$data['sef_schoolyear_id'] = $this->current_school_year;
				$data['sef_created'] =NOW;
				$data['is_uniform_fee'] = 1;
					
				if($c_spr_id !== NULL)
				{
						$for_pb['spb_fee_name'] =$name;
						$for_pb['spb_remaining_balance'] = $rate;
						$for_pb['spb_fee_rate'] = $rate;
						$for_pb['spb_transacted_by'] =$this->session->userdata('userid');
						$for_pb['spb_spr_id'] =$c_spr_id;
						$for_pb['spb_created'] = NOW;
						$for_pb['spb_stud_enrollment_id'] = $enrollment_id_post;
						$for_pb['is_uniform_fee'] = 1;
				}else{
					$for_pb = FALSE;
				}
				
				if($this->M_enrollees->cashier_add_new_ufees($data,$for_pb) == TRUE)
				{
					$this->_msg('s','Uniform Was Successfully added to student','fees/view_fees/'.$id);
				}else{
					$this->_msg('e','An error occured while adding Uniform Fee to student!','fees/view_fees/'.$id);
				}

			}else{
				show_error('Sorry Wrong Address,',404);
			}
		}else{
			show_error('Sorry Wrong Address,',404);
		}
	}
	
	
	public function p($id,$a='')
	{
		$this->load->model('m_print_fees','mpf');
		
		
		// pd($this->mpf->get_student_fees($id));
		if(is_numeric($id) AND $this->authlink->verify_authlink($a))
		{
			$this->load->model('m_print_fees','mpf');
			$this->load->library(array('mpdf'));
			$this->load->helper('misc');
			$data['student_fees'] = $this->mpf->get_student_fees($id);
			$data['payment_breakdown'] = $this->mpf->get_payments_made_with_breakdown($id);
			$data['student_payments'] = $this->mpf->get_payments_made($id);
			$data['sum_paid_fees'] = $this->M_enrollees->get_sum_of_paid_fees($id,$this->current_grading_period,$this->current_school_year);
			$data['sum_amount_due'] = $this->M_enrollees->get_sum_of_amount_due($id,$this->current_school_year);
			$data['rdate'] = function($x){return date('M d, Y h:i A',strtotime($x));};
			$data['enrollee_data'] = $this->M_enrollees->get_enrollee_prof($id);

			$html = $this->load->view('print_pdf_layouts/print_student_fee',$data,TRUE);
			
			$filename = str_replace(' ','_',trim('studentname')).'_'.trim('idnumber');
			$mpdf=new mPDF('c','A4','','' , 10 , 10 , 10 , 10 , 0 , 0);
			$mpdf->SetDisplayMode('fullpage');
			$mpdf->list_indent_first_level = 0;  // 1 or 0 - whether to indent the first level of a list
			$mpdf->WriteHTML($html);
			$mpdf->Output($filename,'I');
		}else{
			redirect($this->current_session_user);
		}
	}
	
	private function _process_student_scholarship($student_id = FALSE,$scholarship_id = FALSE)
	{
	
		// vd(is_numeric($student_id) AND is_numeric($scholarship_id));
	
		if(is_numeric($student_id) AND is_numeric($scholarship_id))
		{
			$this->load->model('M_scholarships','ms');
			return $this->ms->insert_and_process_student_scholarship($student_id,$scholarship_id);
		}else{
			show_error('System Error!, Command Not Found.',404);
		}
	}
	
	public function payment_center()
	{
		$this->load->library('token');
		$this->token->set_token();
		$this->view_data['token'] = $this->token->get_token();
		if($this->input->post('add_payment_record'))
		{
			if($this->token->validate_token($this->input->post('loc_sdi')))
			{
			
				$this->form_validation->set_rules('student','Student ID','required|trim|htmlspecialchars');
				$this->form_validation->set_rules('spr_or_no','OR Number','required|trim|htmlspecialchars');
				$this->form_validation->set_rules('ammount_paid','Amount','required|trim|htmlspecialchars|numeric');
				$this->form_validation->set_rules('date_of_payment','Payment Date','required|trim|datechecker|htmlspecialchars');
				$this->form_validation->set_rules('remarks','remarks','trim|htmlspecialchars');
				$this->form_validation->set_rules('loc_sdi','Invalid Token','required|trim|htmlspecialchars');
				
				if($this->form_validation->run() !== FALSE)
				{
					$student_e_id = $this->M_enrollees->check_id_number($this->input->post('student'));

					if($student_e_id !== FALSE)
					{
					
						$misc_fees = $this->M_enrollees->get_enrollee_fees($student_e_id,$this->current_school_year,'misc');
						$tuit_fees = $this->M_enrollees->get_enrollee_fees($student_e_id,$this->current_school_year,'tuit');
						$other_fees = $this->M_enrollees->get_enrollee_fees($student_e_id,$this->current_school_year,'other');
						
						if($misc AND $tuit AND $other_fees)
						{
							$input['spr_user_trans_id'] = $this->session->userdata('userid');
							$input['spr_user_trans_name'] = $this->session->userdata('username');
							$input['spr_enrollment_id'] = $student_e_id;
							$input['spr_or_no'] = $this->input->post('spr_or_no');
							$input['spr_remarks'] = $this->input->post('remarks');
							$input['spr_payment_date'] =  date('Y-m-d',strtotime($this->input->post('date_of_payment')));
							$input['spr_schoolyear_id'] = $this->current_school_year;// defined in mycontroller
							$input['spr_gperiod_id'] = $this->current_grading_period;// defined in mycontroller
							$input['spr_ammt_paid'] = $this->input->post('ammount_paid');
							$input['spr_created'] = NOW;
							
							if($this->M_student_payment_record->add_payment_record($input) == FALSE)
							{
								$this->authlink->clear_authlink();
								$this->_msg('e','Payment Record was not added','fees/view_fees/'.$student_e_id);
							}else
							{	
								if($this->M_student_payment_record->update_enrollment_record($student_e_id) == TRUE)
								{
									$authlink = $this->authlink->generate_authlink();
									$this->_msg('s','Payment Record was successfully added','fees/payment_breakdown/'.$student_e_id.'/'.$authlink);
								}else{
									$this->authlink->clear_authlink();
									$this->_msg('e','Payment Record was not added','fees/view_fees/'.$student_e_id);
								}
							}
						}else{
							$link = '<a href="'.site_url('fees/view_fees/'.$student_e_id).'" class="btn btn-mini">Set Student Fee</a>';
							$message = 'An error has occured, fee has not been set for this student, please set student fees by clicking the link '.$link.'';
							$this->_msg('p',$message,'fees/payment_center/');
						}
					
					}else{
						$this->_msg('e','Error Student ID Number not in system records.','fees/payment_center');
					}
				}else{
					//show validation errors on view
				}
			}else{
				$this->_msg('e','Submit failed form token Expired, Please try again ','fees/payment_center');
			}
		}
	}
	
	public function set_payment_mode($id = FALSE,$auth = FALSE)
	{
		if(is_numeric($id) AND $this->authlink->verify_authlink($auth))
		{
			$this->load->model('M_fees','mf');
			$this->load->library('token');
			$this->token->set_token();
			
			$this->view_data['enrollee_data'] = $profile = $this->M_enrollees->get_enrollee_prof($id);
			$this->view_data['payment_mode'] = $this->mf->payment_modes($profile->level_id);
			$this->view_data['selected_payment_mode'] = $this->M_enrollees->get_student_payment_mode($id);
			$this->view_data['auth'] = $auth;
			$this->view_data['token'] = $this->token->get_token();
			$this->view_data['id'] = $id;
			
			if($this->input->post('set_student_payment_mode'))
			{
				if($this->token->validate_token($this->input->post('stpr_loc')))
				{
					$this->form_validation->set_message('required','Please choose one from the payment modes below.');
					$this->form_validation->set_rules('selected_payment_mode','Payment Mode','required|trim|alpha');
					if($this->form_validation->run() == FALSE)
					{
						$this->view_data['system_message'] = $this->_msg('e',validation_errors());
					}else{
						$result = $this->mf->set_student_payment_mode($id,$this->input->post('selected_payment_mode'));
						
						// pd($result);
						
						if($result->stat == true)
						{
							$this->_msg('s',$result->log,'fees/set_payment_mode/'.$id.'/'.$auth);
						}else{
							$this->_msg('e',$result->log,'fees/set_payment_mode/'.$id.'/'.$auth);
						}
					}
				}else{
					$this->token->destroy_token();
					$this->_msg('e','Invalid form token.','fees/set_payment_mode/'.$id.'/'.$auth);
				}
			}
		}else{
			show_error('Unable to process you request, invalid entry give to parameters, please don\'t mess with the URL',404);
		}                                        
	}
	*/
	
}